#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib2
import sys
import time
import re
# from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup

reload(sys)
sys.setdefaultencoding('utf-8')

global ret

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]##浏览器的标示

ret=''
city = 'cs' ## 长沙
firstPage = opener.open('https://www.anjuke.com/' + city + '/cm/')
soupFirstPage = BeautifulSoup(firstPage.read())
areaList = soupFirstPage.findAll('a', { 'class' : 'P2a' }) ## 所有区域列表 <a class="P2a" href="https://www.anjuke.com/cs/cm/wangchenga/">望城</a>
print(areaList)
f = file('/Users/fujue/Desktop/community.txt','a')
f.write('字段：区域，小区名称，所属板块，总户数，绿化率，停车位，物业类型，竣工时间')
for value in areaList: ## 遍历行政区
    if value.text.find('全部') == -1: ## 排除全部这个区域
        for i in range(1, 2): ## 如果下一页按钮的class是 nolink nextpage ，表示终结 break 循环pw
            ret=''
            pages = opener.open(value['href'] + '/p' + str(i) ) ## 拼地址
            soup = BeautifulSoup(pages.read())

            allem = soup.findAll('em')

            for em in allem:
                ret=''
                a = em.find('a')
                if a :
                    t = a.text+','  ## 小区名字
                    if t.find('小区大全') == -1:
                        #href = a.href
                        page_cm = opener.open(a.get('href'))
                        soup_cm = BeautifulSoup(page_cm.read())

                        infos_box = soup_cm.find('div', { 'class' : 'infos-box' })
                        if infos_box:
                            info_li =infos_box.select('li span')
                            for li in info_li:
                                t += li.text +','



#
                        ret += t + '\n'
                        print value.text + ' ' + t
       	    f.write(ret)
            print str(i)+' finished'
            nextpageBtn = soup.findAll('span',{ 'class' : 'nolink nextpage' })
            print nextpageBtn
            # print nextpageBtn.ins
            # if nextpageBtn.ins == '下一页':
            #     break
            # if len(soup.select('.nolink')) > 0:
            #     break
f.close()
print 'all community finished'
